//
//  VisitorLoginView.swift
//  ZSYWeiBo
//
//  Created by 周饰宇 on 2017/4/14.
//  Copyright © 2017年 zhouShiYu. All rights reserved.
//

import UIKit

// 协议需要继承自 NSObjectProtocol
protocol VisitorLoginViewDelegate: NSObjectProtocol{
    
    func visitorRegisterButtonClicked()
    func visitorLoginButtonClicked()
}

class VisitorLoginView: UIView {

    ///代理
   weak var delegate: VisitorLoginViewDelegate?
    
    
    /// 设置视图内容
    ///
    /// - Parameters:
    ///   - iconName: 图标名称
    ///   - message:  消息文本
    ///   - isHome:   是否首页
    func setupViewInfo(iconName: String, message: String, isHome:Bool) {
        
        messageLabel.text = message
        
        if isHome {
            
            homeIconView.isHidden = false
            iconView.image = UIImage.init(named: "visitordiscover_feed_image_smallicon")
//            bringSubview(toFront: maskIconView)
        }else{
            
            homeIconView.isHidden = true
            iconView.image = UIImage.init(named: iconName)
            sendSubview(toBack: maskIconView)
        }
        
    }
    
    // 启动动画
    func startAnmation() {
        
        let anmin = CABasicAnimation.init(keyPath: "transform.rotation")
        anmin.toValue = 2 * Double.pi
        anmin.duration = 20
        anmin.repeatCount = MAXFLOAT
        iconView.layer.add(anmin, forKey: nil)
    }
    
    
    // 用户登录
    func login() {
        
        delegate?.visitorLoginButtonClicked()
        
    }
    
    // 用户注册
    func register() {
     
        delegate?.visitorRegisterButtonClicked()
    }
    
    // 用xib或者sb开发不会调用
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    // 解档，用xib或者sb开发会调用
    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK : 创建控件
    private func setupUI(){
        // 1.添加控件
        addSubview(iconView)
        addSubview(homeIconView)
        addSubview(maskIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
       
        
        // 2.自动布局
        iconView.translatesAutoresizingMaskIntoConstraints = false
        
        // 任何一个控件都可以参照宁一个控件
        //
        addConstraint( NSLayoutConstraint.init(item: iconView,attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerX, multiplier: 1.0, constant: 0))
         addConstraint( NSLayoutConstraint.init(item: iconView,attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerY, multiplier: 1.0, constant: -40))
        
        //
        homeIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint( NSLayoutConstraint.init(item: homeIconView,attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: iconView, attribute: NSLayoutAttribute.centerX, multiplier: 1.0, constant: 0))
        addConstraint( NSLayoutConstraint.init(item: homeIconView,attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: iconView, attribute: NSLayoutAttribute.centerY, multiplier: 1.0, constant: 0))
        
        //
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint( NSLayoutConstraint.init(item: messageLabel,attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: iconView, attribute: NSLayoutAttribute.centerX, multiplier: 1.0, constant: 0))
        addConstraint( NSLayoutConstraint.init(item: messageLabel,attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: iconView, attribute: NSLayoutAttribute.bottom, multiplier: 1.0, constant: 0))
        
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[subView(240)]", options: NSLayoutFormatOptions.alignAllLastBaseline, metrics: nil, views: ["subView" : messageLabel]))
        
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[subView(40)]", options: NSLayoutFormatOptions.alignAllLastBaseline, metrics: nil, views: ["subView" : messageLabel]))
        
        //
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint( NSLayoutConstraint.init(item: registerButton,attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: messageLabel, attribute: NSLayoutAttribute.left, multiplier: 1.0, constant: 0))
        
        addConstraint( NSLayoutConstraint.init(item: registerButton,attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: messageLabel, attribute: NSLayoutAttribute.bottom, multiplier: 1.0, constant: 0))
        
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[subView(100)]", options: NSLayoutFormatOptions.alignAllLastBaseline, metrics: nil, views: ["subView" : registerButton]))
        
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[subView(40)]", options: NSLayoutFormatOptions.alignAllLastBaseline, metrics: nil, views: ["subView" : registerButton]))
        
        // 
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint( NSLayoutConstraint.init(item: loginButton,attribute: NSLayoutAttribute.right, relatedBy: NSLayoutRelation.equal, toItem: messageLabel, attribute: NSLayoutAttribute.right, multiplier: 1.0, constant: 0))
        
        addConstraint( NSLayoutConstraint.init(item: loginButton,attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: messageLabel, attribute: NSLayoutAttribute.bottom, multiplier: 1.0, constant: 0))
        
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[subView(100)]", options: NSLayoutFormatOptions.alignAllLastBaseline, metrics: nil, views: ["subView" : loginButton]))
        
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[subView(40)]", options: NSLayoutFormatOptions.alignAllLastBaseline, metrics: nil, views: ["subView" : loginButton]))
        
        
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[subview]|", options: NSLayoutFormatOptions.alignAllFirstBaseline, metrics: nil, views: ["subview" : maskIconView]))
        
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[subview]-160-|", options: NSLayoutFormatOptions.alignAllFirstBaseline, metrics: nil, views: ["subview" : maskIconView]))
        
        backgroundColor = UIColor.init(white: 237.0 / 255.0, alpha: 1.0)
    }
    
    // MARK : lazy
    lazy var iconView : UIImageView = {
        
        let icon = UIImageView(image :  UIImage(named : "visitordiscover_feed_image_smallicon"))
        
        return icon
    }()
    
    lazy var homeIconView : UIImageView = {
        
        let homeImage = UIImage(named : "visitordiscover_feed_image_house")
        return UIImageView(image : homeImage)
    }()
    
    lazy var messageLabel: UILabel = {
        
        // 一旦使用了自动布局，frame 就失效了
        let label = UILabel(frame : CGRect(x : 0 , y : 0, width: 240, height : 40))
        label.text = "关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.darkGray
        label.font = UIFont.systemFont(ofSize: 14)
        label.numberOfLines = 2;
        label.preferredMaxLayoutWidth = 224
        label.textAlignment = NSTextAlignment.center
        label.sizeToFit()
        return label
    }()
    
    lazy var registerButton: UIButton = {
        
        let btn = UIButton(frame :CGRect.init(x: 0, y: 0, width: 100, height: 40));
        btn.setTitle("注册", for: UIControlState.normal)
        btn.setTitleColor(UIColor.orange, for: UIControlState.normal)
        btn.setBackgroundImage(UIImage.init(named: "common_button_white_disable"), for: UIControlState.normal)
        
        btn.addTarget(self, action: #selector(register), for: UIControlEvents.touchUpInside)
        
        return btn
    }()
    
    lazy var loginButton: UIButton = {
        
        let btn = UIButton(frame :CGRect.init(x: 200, y: 0, width: 100, height: 40));
        btn.setTitle("登录", for: UIControlState.normal)
        btn.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        btn.setBackgroundImage(UIImage.init(named: "common_button_white_disable"), for: UIControlState.normal)
        btn.addTarget(self, action: #selector(login), for: UIControlEvents.touchUpInside)
        
        return btn

    }()
    
    
    lazy var maskIconView: UIImageView = {
        
        let image = UIImage.init(named: "visitordiscover_feed_mask_smallicon")
        return UIImageView(image : image)
    }()
    
    
    
    
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
